package cn.wolfcode.luowowo.website.web.controller;


import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import cn.wolfcode.luowowo.website.util.UploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
public class UploadController {

    @Reference
    private IUserInfoService userInfoService;
    @Reference
    private IUserInfoRedisService userInfoRedisService;

    //读取配置文件中的配置
    @Value("${file.path}")
    private String basePath;

    @RequestMapping("/coverImageUpload")
    @ResponseBody
    public Object uploadImg(MultipartFile pic){
        String filename = UploadUtil.upload(pic,basePath);
        return filename;
    }
    @RequestMapping("/upload/file")
    @ResponseBody
    public Object headerImg(MultipartFile files, @UserParam UserInfo userInfo, HttpServletRequest request, HttpServletResponse response){
        System.out.println(files);
        String filename = UploadUtil.upload(files,basePath);
        System.out.println(filename);
        userInfoService.updateHearderById("/"+filename,userInfo.getId());
        String token = userInfoService.getUser(userInfo.getPhone());
        CookieUtil.addCookie(response,"token",token, RedisKeys.USER_LOGIN.getTime().intValue());
        String userStr = userInfoRedisService.getToken(token);
        UserInfo user = JSON.parseObject(userStr, UserInfo.class);
        request.getSession().setAttribute("userInfo",user);
        return filename;
    }

}
